let discovery = {
    init() {
        this.charts={};
        this.ec01_wordCloud_hotWords_a();
        this.ec02_rose_regionalEmotional_a();
        this.ec03_line_dataTrend_a();
        this.ec04_radar_domain_a();
        this.ec05_mediaDataHit_a();
        this.counter = 0;
    },
    ec01_wordCloud_hotWords_a() {
        let that = this;
        let wordCloudData = [
            {
                name: '一带一路',
                value: 85
            }, {
                name: '企业合作',
                value: 82
            }, {
                name: '东盟',
                value: 80
            }, {
                name: '国家能源',
                value: 80
            }, {
                name: '投资',
                value: 77
            }, {
                name: '发展',
                value: 70
            }, {
                name: '国际',
                value: 66
            }, {
                name: '西欧',
                value: 63
            }, {
                name: '国策',
                value: 60
            }];
        $.ajax({
            headers: {
                "token": Public.token//此处放置请求到的用户token
            },
            url: Public.ctx+'/api/domain/focus/hotswords',
            success: (data) => {
                wordCloudData = data.data.maps.sort((a,b)=>b.count-a.count).slice(0,20);//
                ec01_wordCloud_hotWords_f()
            }
        });
        function ec01_wordCloud_hotWords_f() {
            let myChart = $("#ec01_wordCloud_hotWords");
            myChart.empty();
            // that.charts.ec01_wordCloud_hotWords = myChart;
            wordCloudData.forEach(function (item) {
                myChart.append(`<a>${item.name}</a>`)
            });
            var radius = 95*bodyScale;
            var dtr = Math.PI / 180;
            var d = 500;

            var mcList = [];
            var active = false;
            var lasta = 1;
            var lastb = 1;
            var distr = true;
            var tspeed = 5;
            var size = 280;

            var mouseX = 0;
            var mouseY = 0;

            var howElliptical = 1;

            var aA = null;
            var oDiv = null;

            window.onload = function () {
                var i = 0;
                var oTag = null;

                oDiv = document.getElementById('ec01_wordCloud_hotWords');

                aA = oDiv.getElementsByTagName('a');

                for (i = 0; i < aA.length; i++) {
                    oTag = {};

                    oTag.offsetWidth = aA[i].offsetWidth;
                    oTag.offsetHeight = aA[i].offsetHeight;

                    mcList.push(oTag);
                }

                sineCosine(0, 0, 0);

                positionAll();

                oDiv.onmouseover = function () {
                    active = true;
                };

                oDiv.onmouseout = function () {
                    active = false;
                };

                oDiv.onmousemove = function (ev) {
                    var oEvent = window.event || ev;

                    mouseX = oEvent.clientX - (oDiv.offsetLeft + oDiv.offsetWidth / 2);
                    mouseY = oEvent.clientY - (oDiv.offsetTop + oDiv.offsetHeight / 2);

                    mouseX /= 5;
                    mouseY /= 5;
                };

                setInterval(update, 30);
            };

            function update() {
                var a;
                var b;

                if (active) {
                    a = (-Math.min(Math.max(-mouseY, -size), size) / radius) * tspeed;
                    b = (Math.min(Math.max(-mouseX, -size), size) / radius) * tspeed;
                } else {
                    a = lasta * .9999;
                    b = lastb * .9999;
                }

                lasta = a;
                lastb = b;

                if (Math.abs(a) <= 0.01 && Math.abs(b) <= 0.01) {
                    return;
                }

                var c = 0;
                sineCosine(a, b, c);
                for (var j = 0; j < mcList.length; j++) {
                    var rx1 = mcList[j].cx;
                    var ry1 = mcList[j].cy * ca + mcList[j].cz * (-sa);
                    var rz1 = mcList[j].cy * sa + mcList[j].cz * ca;

                    var rx2 = rx1 * cb + rz1 * sb;
                    var ry2 = ry1;
                    var rz2 = rx1 * (-sb) + rz1 * cb;

                    var rx3 = rx2 * cc + ry2 * (-sc);
                    var ry3 = rx2 * sc + ry2 * cc;
                    var rz3 = rz2;

                    mcList[j].cx = rx3;
                    mcList[j].cy = ry3;
                    mcList[j].cz = rz3;

                    per = d / (d + rz3);

                    mcList[j].x = (howElliptical * rx3 * per) - (howElliptical * 2);
                    mcList[j].y = ry3 * per;
                    mcList[j].scale = per;
                    mcList[j].alpha = per;

                    mcList[j].alpha = (mcList[j].alpha - 0.6) * (10 / 6);
                }

                doPosition();
                depthSort();
            }

            function depthSort() {
                var i = 0;
                var aTmp = [];

                for (i = 0; i < aA.length; i++) {
                    aTmp.push(aA[i]);
                }

                aTmp.sort
                (
                    function (vItem1, vItem2) {
                        if (vItem1.cz > vItem2.cz) {
                            return -1;
                        } else if (vItem1.cz < vItem2.cz) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                );

                for (i = 0; i < aTmp.length; i++) {
                    aTmp[i].style.zIndex = i;
                }
            }

            function positionAll() {
                var phi = 0;
                var theta = 0;
                var max = mcList.length;
                var i = 0;

                var aTmp = [];
                var oFragment = document.createDocumentFragment();

                //随机排序
                for (i = 0; i < aA.length; i++) {
                    aTmp.push(aA[i]);
                }

                aTmp.sort
                (
                    function () {
                        return Math.random() < 0.5 ? 1 : -1;
                    }
                );

                for (i = 0; i < aTmp.length; i++) {
                    oFragment.appendChild(aTmp[i]);
                }

                oDiv.appendChild(oFragment);

                for (var i = 1; i < max + 1; i++) {
                    if (distr) {
                        phi = Math.acos(-1 + (2 * i - 1) / max);
                        theta = Math.sqrt(max * Math.PI) * phi;
                    } else {
                        phi = Math.random() * (Math.PI);
                        theta = Math.random() * (2 * Math.PI);
                    }
                    //坐标变换
                    mcList[i - 1].cx = radius * Math.cos(theta) * Math.sin(phi);
                    mcList[i - 1].cy = radius * Math.sin(theta) * Math.sin(phi);
                    mcList[i - 1].cz = radius * Math.cos(phi);

                    aA[i - 1].style.left = mcList[i - 1].cx + oDiv.offsetWidth / 2 - mcList[i - 1].offsetWidth / 2 + 'px';
                    aA[i - 1].style.top = mcList[i - 1].cy + oDiv.offsetHeight / 2 - mcList[i - 1].offsetHeight / 2 + 'px';
                }
            }

            function doPosition() {
                var l = oDiv.offsetWidth / 2;
                var t = oDiv.offsetHeight / 2;
                for (var i = 0; i < mcList.length; i++) {
                    aA[i].style.left = mcList[i].cx + l - mcList[i].offsetWidth / 2 + 'px';
                    aA[i].style.top = mcList[i].cy + t - mcList[i].offsetHeight / 2 + 'px';

                    aA[i].style.fontSize = Math.ceil(12 * mcList[i].scale / 2) + 8 + 'px';

                    aA[i].style.filter = "alpha(opacity=" + 100 * mcList[i].alpha + ")";
                    aA[i].style.opacity = mcList[i].alpha;
                }
            }

            function sineCosine(a, b, c) {
                sa = Math.sin(a * dtr);
                ca = Math.cos(a * dtr);
                sb = Math.sin(b * dtr);
                cb = Math.cos(b * dtr);
                sc = Math.sin(c * dtr);
                cc = Math.cos(c * dtr);
            }
        }

    },
    ec02_rose_regionalEmotional_a() {
        let that = this;
        $.ajax({
            headers: {
                "token": Public.token//此处放置请求到的用户token
            },
            url: Public.ctx + '/api/aggregate/discovery/areaEmotional',
            type: 'get',
            dataType: 'json',
            success: (data) => {
                if (data.success) {
                    var country = [];
                    var jiji = [];
                    var zhongxing = [];
                    var xiaoji = [];
                    $.each(data.data.maps, function (i, item) {
                        country.push(item.name);
                        jiji.push(item.details[1].nums);
                        zhongxing.push(item.details[2].nums);
                        xiaoji.push(item.details[0].nums);
                    });

                    ec02_rose_regionalEmotional_d(country, jiji, zhongxing, xiaoji);
                } else {
                    // Public.message(data);
                }
            }
        });

        function ec02_rose_regionalEmotional_d(xData, jijiData, zhongxingData, xiaojiData) {
            var dataRose = echarts.init($("#ec02_rose_regionalEmotional")[0]);
            that.charts.ec02_rose_regionalEmotional = dataRose;
            var app = {};
            option = null;
            app.title = '区域情感分布';
            option = {

                legend: {
                    icon: "rect ",
                    show: true,
                    y2: '6%',
                    itemWidth: 6 * bodyScale,
                    itemHeight: 6 * bodyScale,
                    itemGap: 30 * bodyScale,
                    textStyle: {
                        color: '#fff'
                    },
                    rich: {
                        b: {color: 'red'}
                    },
                    data: [{
                        name: '积极', rich: {
                            b: {color: 'red'}
                        }
                    }, '中性', '消极']
                },
                color: ['#43C4C4', '#9639CD', '#0C8DF9'],
                angleAxis: {
                    type: 'category',
                    data: xData,
                    z: 10,
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        show: false
                    },
                    axisLabel: {
                        interval: 0,
                        show: true,
                        color: "#fff",
                        margin: 8 * bodyScale,
                        fontSize: 12 * bodyScale
                    }
                },
                radiusAxis: {
                    axisLine: {
                        show: false,
                        lineStyle: {
                            color: 'rgba(255,0,0,0)'
                        }
                    },
                    splitLine: {
                        lineStyle: {
                            show: false,
                            color: "#00f",
                            width: 0,
                            type: "solid"
                        }
                    }
                },
                polar: {
                    center: ['50%', '40%'],
                    radius: '55%',
                },
                series: [{
                    type: 'bar',
                    data: jijiData,
                    coordinateSystem: 'polar',
                    name: '积极',
                    stack: 'a'
                }, {
                    type: 'bar',
                    data: zhongxingData,
                    coordinateSystem: 'polar',
                    name: '中性',
                    stack: 'a'
                }, {
                    type: 'bar',
                    data: xiaojiData,
                    coordinateSystem: 'polar',
                    name: '消极',
                    stack: 'a'
                }]
            };
            if (option && typeof option === "object") {
                dataRose.setOption(option, true);
            }
        }
    },
    ec03_line_dataTrend_a() {
        let myChart = echarts.init($('#ec03_line_dataTrend')[0]);
        this.charts.ec03_line_dataTrend = myChart;
        $.ajax({
            headers: {
                "token": Public.token//此处放置请求到的用户token
            },
            url: Public.ctx + '/api/common/innter?dataType=yeesight_pc_homepage_trend',
            type: 'get',
            dataType: 'json',
            success: (data) => {
                if (data.msg === 'success') {
                    let dataObj = data.data;
                    let legendArr = []
                    let xAxisArr = []
                    let yAxisArr = []
                    // console.log('dataObj', dataObj)
                    Object.keys(dataObj).forEach((item, index) => {
                        legendArr.push({
                            name: dataObj[item][0]["legend"],
                            icon: 'rect'
                        });
                        xAxisArr = []
                        let axisY = []
                        dataObj[item].forEach(item => {
                            xAxisArr.push(item.axisX);
                            axisY.push(Number(item.axisY / 10000).toFixed(2));
                        })
                        yAxisArr.push(axisY)
                    })
                    let series = [];
                    let colors = ["#008aff", "#40e9ea"];
                    yAxisArr.forEach((item, index) => {
                        series.push({
                            name: legendArr[index].name,
                            symbol: 'circle',
                            symbolSize: 3 * bodyScale,
                            type: 'line',
                            smooth: true,
                            areaStyle: {
                                // opacity: '.1',
                                color: {
                                    type: 'linear',
                                    x: 0,
                                    y: 0,
                                    x2: 0,
                                    y2: 1,
                                    colorStops: [{
                                        offset: 0.3, color: colors[index] + '30'// 0% 处的颜色
                                    }, {
                                        offset: 1, color: 'rgba(0,138,255,0)' // 100% 处的颜色
                                    }],
                                    global: false // 缺省为 false
                                }
                            },
                            lineStyle: {
                                width: 1.5 * bodyScale
                            },
                            data: item
                        })
                    })
                    myChart.setOption({
                        color: colors,
                        tooltip: {
                            trigger: 'axis',
                            backgroundColor: '#008aff30',
                            axisPointer: {
                                type: 'cross',
                                label: {
                                    backgroundColor: '#6a7985'
                                }
                            }
                        },
                        legend: {
                            data: legendArr,
                            right: '5%',
                            top: 10 * bodyScale,
                            itemHeight: 6 * bodyScale,
                            itemWidth: 6 * bodyScale,
                            itemGap: 20 * bodyScale,
                            textStyle: {
                                color: "#fff"
                            }
                        },
                        grid: {
                            left: '15%',
                            right: '10%',
                            top: '16%',
                            bottom: '17%',
                        },
                        xAxis: [
                            {
                                boundaryGap: false,
                                type: 'category',
                                data: xAxisArr,
                                axisTick: {
                                    show: false
                                },
                                nameTextStyle: {
                                    color: "#fff"
                                },
                                axisLine: {
                                    show: false,
                                    // color: this.$colorSplitLine
                                },
                                axisLabel: {
                                    fontSize: 10 * bodyScale,
                                    color: '#fff'
                                }
                            }
                        ],
                        yAxis: [
                            {
                                name: '数据（/万条）',
                                type: 'value',
                                axisTick: {
                                    show: false
                                },
                                splitLine: {
                                    show: false
                                },
                                nameTextStyle: {
                                    color: "#fff"
                                },
                                axisLine: {
                                    show: false,
                                    // color: this.$colorSplitLine
                                },
                                axisLabel: {
                                    fontSize: 10 * bodyScale,
                                    color: '#fff'
                                }
                            }
                        ],
                        series,
                    })
                    myChart.resize()
                }
            }
        })

    },
    ec04_radar_domain_a() {
        let myChart = echarts.init($("#ec04_radar_domain")[0]);
        this.charts.ec04_radar_domain = myChart;
        $.ajax({
            headers: {
                "token": Public.token//此处放置请求到的用户token
            },
            url: Public.ctx + '/api/common/innter?dataType=yeesight_pc_homepage_category',
            type: 'get',
            dataType: 'json',
            success: (data) => {
                if (data.msg === 'success') {
                    console.log('ec04_radar_domain_a',data)
                    let obj = data.data
                    let legendArr = []
                    let indicator = []
                    let seriesData = []
                    let valueArr = []
                    let selectedArr = {};
                    Object.keys(obj).forEach((item, index) => {
                        legendArr = []
                        let temp = []
                        obj[item].forEach(item => {
                            legendArr.push(item.name)
                            temp.push(isNaN(item.value) ? '0' : item.value)
                        })
                        indicator.push({
                            name: obj[item][0].class,
                            max: isNaN(obj[item][0].max) ? '10000' : obj[item][0].max
                        })
                        valueArr.push(temp)
                    })

                    let newArray = valueArr[0].map(function (col, i) {
                        return valueArr.map(function (row) {
                            return row[i];
                        })
                    });

                    legendArr.forEach((item, index) => {

                        seriesData.push({
                            value: newArray[index],
                            name: item
                        })
                        if (index > 12) {
                            selectedArr[item] = false
                        }
                    })

                    myChart.setOption({
                        color: ['#1882ca', '#b4282c', '#cd2b68', '#bc834e', '#c4bc39', '#72be76', '#903e21', '#233fd2', '#781ed2', '#15c9ae',],
                        title: {
                            // text: "领域分布",
                            textStyle: {
                                color: "#008aff",
                                fontSize: '16',
                                fontWeight: 300
                            },
                        },
                        grid: {
                            // left: '20%',
                            // right: '20%',
                            // bottom: '50%',

                        },
                        tooltip: {
                            show: true,
                            backgroundColor: '#008aff30',
                        },
                        legend: {
                            pageIconColor: '#008aff',
                            pageIconInactiveColor: '#008aff50',//翻页按钮不激活的颜色
                            pageTextStyle: {
                                color: '#fff',
                            },
                            data: legendArr,
                            textStyle: {
                                color: '#fff'
                            },
                            itemHeight: 6 * bodyScale,
                            itemWidth: 6 * bodyScale,
                            bottom: "5%",
                            itemGap: 15 * bodyScale,
                            selected: selectedArr
                        },
                        radar: {
                            // shape: 'circle',
                            radius: '48%',
                            center: ['50%', '40%'],
                            name: {
                                textStyle: {
                                    color: "#fff",
                                    // backgroundColor: "#999",
                                    borderRadius: 3,
                                    padding: [3, 5]
                                }
                            },
                            axisLine: {
                                lineStyle: {
                                    color: 'rgba(0, 138, 255, 0.5)'
                                }
                            },
                            splitLine: {
                                lineStyle: {
                                    color: 'rgba(0, 138, 255, 0.5)'
                                }
                            },
                            splitArea: {
                                areaStyle: {
                                    color: ['rgba(0, 138, 255, 0)', 'rgba(0, 138, 255, 0.1)']
                                }
                            },
                            indicator: indicator
                        },
                        series: [
                            {
                                symbol: 'none',
                                name: "领域分布",
                                type: "radar",
                                // areaStyle: {normal: {}},
                                lineStyle: {
                                    width: 2
                                },
                                data: seriesData
                            }
                        ]
                    })
                    myChart.resize()
                }
            }
        })
    },
    ec05_mediaDataHit_a(){
        let that = this;
        $.ajax({
            headers: {
                "token": Public.token//此处放置请求到的用户token
            },
            url: Public.ctx + '/api/aggregate/discovery/media',
            type: 'get',
            dataType: 'json',
            success: (data) => {
                if(data.success){
                    // console.log(data)
                    $(function () {
                        // that.counter
                    })
                    data = data.data.maps.slice(0,6);
                    let html = '';
                    data.forEach(item=>{
                        html+=`<li><span>${item.name}</span><span>${item.count}</span></li>`
                    });
                    $("#mediaDataHit").html(html);
                }
            }
        })
    }
};
discovery.init();
